package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PChatListBo;
import com.ruoyi.system.domain.vo.PChatListVo;
import com.ruoyi.system.domain.PChatList;
import com.ruoyi.system.mapper.PChatListMapper;
import com.ruoyi.system.service.IPChatListService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 聊天列Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-22
 */
@RequiredArgsConstructor
@Service
public class PChatListServiceImpl implements IPChatListService {

    private final PChatListMapper baseMapper;

    /**
     * 查询聊天列
     */
    @Override
    public PChatListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询聊天列列表
     */
    @Override
    public TableDataInfo<PChatListVo> queryPageList(PChatListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PChatList> lqw = buildQueryWrapper(bo);
        Page<PChatListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询聊天列列表
     */
    @Override
    public List<PChatListVo> queryList(PChatListBo bo) {
        LambdaQueryWrapper<PChatList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PChatList> buildQueryWrapper(PChatListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PChatList> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getChatId() != null, PChatList::getChatId, bo.getChatId());
        lqw.eq(bo.getAnotherId() != null, PChatList::getAnotherId, bo.getAnotherId());
        lqw.eq(bo.getUserId() != null, PChatList::getUserId, bo.getUserId());
        lqw.eq(bo.getStatus() != null, PChatList::getStatus, bo.getStatus());
        lqw.eq(bo.getIsOnline() != null, PChatList::getIsOnline, bo.getIsOnline());
        lqw.eq(bo.getUnread() != null, PChatList::getUnread, bo.getUnread());
        return lqw;
    }

    /**
     * 新增聊天列
     */
    @Override
    public Boolean insertByBo(PChatListBo bo) {
        PChatList add = BeanUtil.toBean(bo, PChatList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改聊天列
     */
    @Override
    public Boolean updateByBo(PChatListBo bo) {
        PChatList update = BeanUtil.toBean(bo, PChatList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PChatList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除聊天列
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }



    @Override
    public List<Map<String, Object>> chatLists(Long userId,int status) {
        return baseMapper.chatLists(userId,status);
    }

    @Override
    public List<Map<String, Object>> serviceMessage(Long userId) {
        return baseMapper.serviceMessage(userId);
    }

    @Override
    public List<Map<String, Object>> writtenAgent(Long userId) {
        return baseMapper.writtenAgent(userId);
    }

    @Override
    public List<Map<String, Object>> telephoneAgent(Long userId) {
        return baseMapper.telephoneAgent(userId);
    }

    @Override
    public List<Map<String, Object>> consultingOrder(Long userId,Integer status) {
        return baseMapper.consultingOrder(userId,status);
    }


    @Override
    public PChatList chatInt(Long userId,int status) {
        return baseMapper.chatInt(userId,status);
    }
}
